<?php
/**
 *	  商户管理控制器
 *	  by Ouyoung
 *
 */
Core::load_class('init', 'admin');
class admin_control extends init_control {
   	public function _initialize() {
		parent::_initialize();
		$this->supplier_db = $this->load->table('supplier');
		$this->supplier_service = $this->load->service('supplier');
        
		$this->group_service = $this->load->service('supplier_group');
		$this->group_model = $this->load->table('supplier_group');  
    }
    
	/**
	 * 获取商户列表
	 */
	public function index() {
		$sqlmap = array();
		$_GET['limit'] = (isset($_GET['limit']) && is_numeric($_GET['limit'])) ? $_GET['limit'] : 20;
		
        $lists = $this->supplier_service->get_lists($_GET);
        // $lists = $this->supplier_db->getField('id,company,group_id');
        // $lists = $this->supplier_db->field('id,company,group_id')->select();
        $count = $lists['count'];
        $supplier = $lists['lists'];
      
                
        $pages = $this->admin_pages($count, $_GET['limit']);
		$this->load->librarys('View')->assign('supplier',$supplier);
		$this->load->librarys('View')->assign('pages',$pages);
		$this->load->librarys('View')->display('index');
	}
    
	/**
	 * 添加商户
	 */
	public function add() {
		$group = $this->group_service->getlist();
		if(!$group)showmessage(lang('supplier/_no_group_list_'),url('group_add'),0);
		$group_format = format_select_data($group['group']);
		if (checksubmit('dosubmit')) {
			$r = $this->supplier_service->save($_GET);
			if($r == FALSE)showmessage($this->supplier_db->getError(), url('add'), 0);
			showmessage(lang('supplier/_save_supplier_success_'), url('index'), 1);
		} else {
			$this->load->librarys('View')->assign('group',$group['group']);
			$this->load->librarys('View')->assign('group_format',$group_format);
			$this->load->librarys('View')->display('update');
		}
	}

	/**
	 * 编辑商户
	 */
	public function edit() {
		$group = $this->group_service->getlist();
		$group_format = format_select_data($group);
		$data = $this->supplier_service->fetch_by_id($_GET['id']);
		if (checksubmit('dosubmit')) {
			$r = $this->supplier_service->save($_GET);
			showmessage(lang('supplier/_update_supplier_success_'), url('index'), 1);
		} else {
			$this->load->librarys('View')->assign('group',$group);
			$this->load->librarys('View')->assign('group_format',$group_format);
			extract($data);
			$this->load->librarys('View')->assign($data,$data);
			$this->load->librarys('View')->display('update');
		}
	}

	/**
	 * [ajax_del 删除商品，在商家列表里删除只改变状态]
	 * @return [type]         [description]
	 */
	public function ajax_del(){
        if(empty($_GET['formhash']) || $_GET['formhash'] != FORMHASH) showmessage('_token_error_');
		$result = $this->supplier_service->ajax_del($_GET);
		if(!$result){
			showmessage($this->supplier_service->error);
		}else{
			showmessage(lang('_operation_success_'),url('index'),1);
		}
	}
    
	/**
	 * [ajax_recover 恢复商家]
	 * @param  [array] $id [要恢复的商家id]
	 * @return [type]     [description]
	 */
	public function ajax_recover(){
		$result = $this->supplier_service->ajax_recover($_GET['id']);
		if(!$result){
			showmessage($this->supplier_service->error);
		}else{
			showmessage(lang('_operation_success_'),url('index'),1);
		}
	}

	/**
	 * 编辑商家名称
	 */
 	public function ajax_save_name(){
        if(empty($_GET['formhash']) || $_GET['formhash'] != FORMHASH) showmessage('_token_error_');
		$result = $this->supplier_service->ajax_save_name($_GET);
		if(!$result){
			showmessage($this->supplier_service->error);
		}else{
			showmessage(lang('supplier/_update_supplier_name_success_'), url('index'), 1);
		}
	}
    
	/**
	 * 获取商户组列表
	 */
	public function group() {
		$sqlmap = array();
        $sqlmap['status'] = $_GET['label'] ? -1 : array('neq',-1);
		$supplier_group = $this->group_service->getlist($sqlmap);
		$pages = $this->admin_pages($supplier_group['count'], 10);
		$this->load->librarys('View')->assign('supplier_group',$supplier_group['group']);
		$this->load->librarys('View')->assign('pages',$pages);
		$this->load->librarys('View')->display('group');
	}
    
    /**
	 * 添加商户组列表
	 */
	public function add_group() {
		$group = $this->group_service->getlist();
		if(!$group)showmessage(lang('supplier/_no_group_list_'),url('group_add'),0);
		$group_format = format_select_data($group['group']);
        
		if (checksubmit('dosubmit')) {
			$r = $this->group_service->save($_GET);
			if($r == FALSE)showmessage($this->group_model->getError(), url('group_add'), 0);
			showmessage(lang('supplier/_save_supplier_success_'), url('group'), 1);
		} else {
			$this->load->librarys('View')->assign('group',$group['group']);
			$this->load->librarys('View')->assign('group_format',$group_format);
			$this->load->librarys('View')->display('group_add');
		}
	}
    
    /**
	 * 编辑商户组
	 */
	public function edit_group() {
		$data = $this->group_service->fetch_by_id($_GET);
		if (checksubmit('dosubmit')) {
			$r = $this->group_service->save($_GET);
			showmessage(lang('supplier/_update_supplier_success_'), url('group'), 1);
		} else {
			$this->load->librarys('View')->assign($data,$data);
			$this->load->librarys('View')->display('group_add');
		}
	}
    
     /**
	 * 删除商户组
	 */
    public function del_group() { 
        $result = $this->group_service->ajax_del($_GET['id']);
        showmessage('_operation_success_',url('group'),1);
    }
    
	/**
	 * ajax修改商家组名称
	 */
 	public function ajax_save_group_name() {
        if(empty($_GET['formhash']) || $_GET['formhash'] != FORMHASH) showmessage('_token_error_');
		$result = $this->group_service->ajax_save_name($_GET);
		if(!$result){
			showmessage($this->group_service->error);
		}else{
			showmessage(lang('supplier/_update_supplier_group_name_success_'), url('group'), 1);
		}
	}
    
    
	/**
	 * [ajax_recover 恢复商家分组]
	 * @param  [array] $id [要恢复的分组id]
	 * @return [type]     [description]
	 */
	public function ajax_recover_group(){
		$result = $this->group_service->ajax_recover($_GET['id']);
		if(!$result){
			showmessage($this->group_service->error);
		}else{
			showmessage(lang('_operation_success_'),url('group'), 1);
		}
	}
}